# -*- coding: utf-8 -*-
# @Author: Administrator
# @Date:   2018-08-01 23:10:40
# @Last Modified by:   Administrator
# @Last Modified time: 2018-08-05 23:30:59

import os
import time
import urllib.request
import json
import requests
from time import sleep
from selenium import webdriver
from selenium.webdriver.common.keys import Keys

# 网页请求链接
def curlget(number):
    headers = {'User-Agent': 'Mozilla/5.0 (Windows NT 6.1; WOW64; rv:23.0) Gecko/20100101 Firefox/23.0'};
    url = "http://www.hualongxiang.com/getThreadInfo.php?tid=" + str(number);
    req = requests.get(url, headers=headers);
    req.encoding = 'gbk';
    return req.text.split('***')[2];



# 打开浏览器
options = webdriver.ChromeOptions();
# 手机浏览器
# mobile_emulation = {"deviceName":"iPhone 6"}
# options.add_experimental_option("mobileEmulation", mobile_emulation);
browser = webdriver.Chrome(executable_path='chromedriver.exe', chrome_options=options);
browser.set_window_size(1366, 768);
req_url = "http://www.hualongxiang.com";
browser.get(req_url);
sleep(2);

# 登陆 userName
browser.find_element_by_css_selector(".btn_login").click();
browser.find_element_by_css_selector("#pop_login_un").send_keys("15951227089");
browser.find_element_by_css_selector("#pop_login_pwd").send_keys("aa123456");
browser.find_element_by_css_selector("#pop_login_btn").click();
print('登陆成功！');
sleep(2);

# 点击链接跳转
# browser.find_element_by_css_selector(".hot_list li h2 a").click();
# print('跳转成功！');
# sleep(2);



# 发帖函数
def fatie():
    current_window = browser.current_window_handle;
    all_window = browser.window_handles;
    for window in all_window:
        if window != current_window:
            browser.switch_to.window(window)

    # content = "想知道你喜欢的明星行程吗？关注微信公众号：星程早班车，发送明星名字，一切了如指掌！[img]http://chuantu.biz/t6/351/1532962118x-1404755576.jpg[/img]";
    content = '楼主别的不多说，就给你个666666';

    browser.find_element_by_css_selector("#textarea").send_keys(content);
    browser.find_element_by_css_selector("#submit").click();



for num in range(100):
    with open('D:/wamp64/www/selenium/tid.txt', 'r', encoding='utf-8') as f:
        tid = f.read();
    sleep(2);
    number = 1 + int(tid);
    with open('D:/wamp64/www/selenium/tid.txt', 'w', encoding='utf-8') as f:
        f.write(str(number));
    f.close();
    newurl = curlget(number);
    print(newurl);
    if (newurl == "http://www.hualongxiang.com/nofid/" + str(number)):
        print("http://www.hualongxiang.com/nofid/" + str(number));
        exit;
    browser.get(newurl);
    sleep(2);
    content = "想知道你喜欢的明星行程吗？关注微信公众号：星程早班车，发送明星名字，一切了如指掌！[img]http://chuantu.biz/t6/351/1532962118x-1404755576.jpg[/img]";
    browser.find_element_by_css_selector("#textarea").send_keys(content);
    browser.find_element_by_css_selector("#submit").click();

exit;
# 输出写入的内容
# searchtxt = browser.execute_script("return document.getElementById('searchtxt').value");print(searchtxt);
# browser.execute_script("a = document.getElementById('searchtxt').value; alert(a);");
# browser.find_element_by_xpath("/html/body/div[3]/div[4]/div/div[3]/div[4]/h3/a").click()
# browser.find_element_by_css_selector(".hot_list li h2 a").click();
# searchtxt = browser.execute_script("alert(999);");

# get请求数据